<template>
  <BaseDialog :title="(!props.item.keyId ? '新增' : '编辑') + '标签'" :handleCancel="handleCancel"
    :dialogVisible="props.dialogVisible">
    <BaseForm labelWidth="100px" type="rule" :handleOk="handleOk" :handleCancel="handleCancel" :items="formItems"
      v-model:form="ruleForm" :rules="rules" :refresh="handleSearch">
    </BaseForm>
  </BaseDialog>
</template>
  
<script setup lang="ts">
import { contentManagementService } from '@/api/contentManagement'
import { tagService } from '@/api/tag'
const cateList = ref([])
const props = defineProps({
  dialogVisible: {
    type: Boolean,
  },
  item: {
    type: Object,
  },
  handleSearch: {
    type: Function,
  },
  /* 1-用户标签 2-群标签 */
  type: {
    type: String,
  },
  searchParams: {
    type: Object,
  },
})
const emit = defineEmits(['update:dialogVisible'])

const formItems = ref([
  {
    name: '所属分类',
    key: 'categoryId',
    type: 'select',
    options: cateList,
  },
  {
    name: '标签名称',
    key: 'tagName',
    type: 'input',
    maxlength: '205',
    placeholder: '请输入',
  },

])
const rules = reactive({
  categoryId: [{ required: true, message: '请选择', trigger: 'change' }],
  tagName: [{ required: true, message: '请输入', trigger: 'change' }],
  name: [{ required: true, message: '请输入', trigger: 'change' }],
  isDisable: [{ required: true, message: '请选择', trigger: 'change' }],
})
const ruleForm = ref({
  tagId: '',
  tagName: '',
  categoryId: '',
  remark: '',

})
const handleOk = () => {
  if (props.item.tagId) {
    return tagService.updateTag({
      data: { ...ruleForm.value },
    })
  } else {
    return tagService.addTag({
      data: { ...ruleForm.value, type: props.type },
    })
  }
}

const handleCancel = () => {
  emit('update:dialogVisible', false)
}
const getCategoryList = () => {
  tagService.getCategoryList({ data: { type: props.type } }).then((res) => {
    cateList.value = res.data.map((e) => {
      return {
        label: e.categoryName,
        value: e.categoryId,
      }
    })

  })
}
onMounted(() => {
  getCategoryList();
  if (props.item.tagId) {
    ruleForm.value.tagId = props.item.tagId
    ruleForm.value.tagName = props.item.tagName
    ruleForm.value.categoryId = props.item.categoryId

  } else {
    ruleForm.value.categoryId = props.searchParams.categoryId
  }
})
</script>
  
<style lang="scss" scoped></style>
  